﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Management;

namespace CoolCode.Web.Security
{
    /// <summary>
    /// 日志记录
    /// </summary>
    public class LinqWebEventProvider : WebEventProvider
    {
        // The current number of buffered messages 
        private int msgCounter;

        // The max number of messages to buffere.
        private int maxMsgNumber;

        // The message buffer.
        private Queue<WebBaseEvent> msgBuffer = new Queue<WebBaseEvent>();

        public LinqWebEventProvider()
            : base()
        {  
            //TODO: Log Event
            // Clear the message buffer.
            msgBuffer.Clear();

            // Initialize the max number of messages
            // to buffer.
            maxMsgNumber = 10;

        } 

        public override void Flush()
        {
            //TODO: Save Event
            msgCounter = 0;
            // Clear the buffer.
            msgBuffer.Clear();
        }

        
        // Process the event that has been raised.
        public override void ProcessEvent(WebBaseEvent raisedEvent)
        {
            if (msgCounter < maxMsgNumber)
            {
                // Buffer the event information.
                msgBuffer.Enqueue(raisedEvent);
                // Increment the message counter.
                msgCounter += 1;
            }
            else
            {
                // Flush the buffer.
                Flush();
            }
        }

        public override void Shutdown()
        {
            Flush();
        }

    }
}
